﻿// CDepartment.cpp: 实现文件
//

#include "pch.h"
#include "HospitalSystem.h"
#include "CDepartment.h"
#include "department.h"
#include "doctor.h"

// CDepartment

IMPLEMENT_DYNCREATE(CDepartment, CFormView)

CDepartment::CDepartment()
	: CFormView(IDD_CDepartment)
	, m_add1(_T(""))
	, m_add2(_T(""))
	, m_add3(_T(""))
	, m_add4(_T(""))
	, m_add5(_T(""))
	, m_xiugai1(_T(""))
	, m_xiugai2(_T(""))
	, m_xiugai3(_T(""))
	, m_xiugai4(_T(""))
	, m_xiugai5(_T(""))
	, m_delete(_T(""))
{

}

CDepartment::~CDepartment()
{
}

void CDepartment::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST1, m_deplist);
	DDX_Text(pDX, IDC_EDIT2, m_add1);
	DDX_Text(pDX, IDC_EDIT1, m_add2);
	DDX_Text(pDX, IDC_EDIT6, m_add3);
	DDX_Text(pDX, IDC_EDIT3, m_add4);
	DDX_Text(pDX, IDC_EDIT5, m_add5);
	DDX_Text(pDX, IDC_EDIT8, m_xiugai1);
	DDX_Text(pDX, IDC_EDIT7, m_xiugai2);
	DDX_Text(pDX, IDC_EDIT10, m_xiugai3);
	DDX_Text(pDX, IDC_EDIT9, m_xiugai4);
	DDX_Text(pDX, IDC_EDIT12, m_xiugai5);
	//  DDX_Text(pDX, IDC_EDIT11, m_delete);
	//  DDX_Text(pDX, IDC_EDIT11, m_delete);
	DDX_Text(pDX, IDC_EDIT11, m_delete);
}

BEGIN_MESSAGE_MAP(CDepartment, CFormView)
	ON_BN_CLICKED(IDC_BUTTON5, &CDepartment::OnBnClickedButton5)
	ON_BN_CLICKED(IDC_BUTTON6, &CDepartment::OnBnClickedButton6)
	ON_BN_CLICKED(IDC_BUTTON7, &CDepartment::OnBnClickedButton7)
	ON_BN_CLICKED(IDC_BUTTON4, &CDepartment::OnBnClickedButton4)
	ON_BN_CLICKED(IDC_BUTTON1, &CDepartment::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON2, &CDepartment::OnBnClickedButton2)
END_MESSAGE_MAP()


// CDepartment 诊断

#ifdef _DEBUG
void CDepartment::AssertValid() const
{
	CFormView::AssertValid();
}

#ifndef _WIN32_WCE
void CDepartment::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif
#endif //_DEBUG


// CDepartment 消息处理程序


void CDepartment::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	CString field[] = { L"科室编号",L"科室名",L"科室地址",L"联系电话" ,L"主任编号" };
	for (int i = 0; i < sizeof(field) / sizeof(field[0]); ++i)
	{
		m_deplist.InsertColumn(i, field[i], LVCFMT_CENTER, 80);
	}
	CString strsql;
	strsql.Format(L"select * from department");
	List_All(strsql);
}


void CDepartment::OnBnClickedButton5()
{
	// TODO: 在此添加控件通知处理程序代码
	Department m_cd;
	if (m_cd.IsOpen())
	{
		m_cd.Close();
	}
	m_cd.Open(CRecordset::dynaset);
	
	UpdateData(true);
	while (!m_cd.IsEOF())
	{
		
		if (m_add1 == m_cd.m_dep_id)
		{
			MessageBox(L"改科室已存在！");
			return;
		}
		m_cd.MoveNext();
	}
	m_cd.AddNew();
	m_cd.m_dep_id = m_add1;
	m_cd.m_dep_name = m_add2;
	m_cd.m_dep_address = m_add3;
	m_cd.m_dep_tep = m_add4;
	m_cd.m_dep_dir = m_add5;
	if (m_cd.Update())
	{
		MessageBox(L"添加成功！");
		CString strsql;
		strsql.Format(L"select * from department");
		List_All(strsql);
	}
	else
	{
		MessageBox(L"添加失败！");
	}
	m_cd.Close();
}


void CDepartment::OnBnClickedButton6()
{
	// TODO: 在此添加控件通知处理程序代码
	m_add1.Empty();
	m_add2.Empty();
	m_add3.Empty();
	m_add4.Empty();
	m_add5.Empty();
	UpdateData(FALSE); //把数据更新到控件上
}


void CDepartment::OnBnClickedButton7()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(true);
	Department m_cd;
	m_cd.Open(CRecordset::dynaset);
	if (!m_cd.CanUpdate())
	{
		MessageBox(L"不可修改！");
		return;
	}
	m_cd.MoveFirst();
	while (!m_cd.IsEOF())
	{
		m_cd.MoveNext();
		if (m_xiugai1 == m_cd.m_dep_id)
		{
			m_cd.Edit();
			m_cd.m_dep_name = m_xiugai2;
			m_cd.m_dep_address = m_xiugai3;
			m_cd.m_dep_tep = m_xiugai4;
			m_cd.m_dep_dir = m_xiugai5;
			m_cd.Update();
			MessageBox(L"修改成功");
			CString strsql;
			strsql.Format(L"select * from department");
			List_All(strsql);
			return;
		}
	}

	MessageBox(L"不存在改科室！");
}


void CDepartment::OnBnClickedButton4()
{
	// TODO: 在此添加控件通知处理程序代码
	m_xiugai2.Empty();
	m_xiugai3.Empty();
	m_xiugai4.Empty();
	m_xiugai5.Empty();
	UpdateData(FALSE); //把数据更新到控件上
}


void CDepartment::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
	// TODO: 在此添加控件通知处理程序代码
	Department m_cd;
	Doctor m_dr;
	m_dr.Open(CRecordset::dynaset);
	m_cd.Open(CRecordset::dynaset);
	UpdateData(true);
	m_cd.MoveFirst();
	m_dr.MoveFirst();
	while (!m_dr.IsEOF())
	{
		if (m_dr.m_dep_id == m_delete)
		{
			m_dr.Close();
			m_cd.Close();
			MessageBox(L"不能删除，该科室还有医生！");
			return;
		}
		m_dr.MoveNext();
	}
	while (!m_cd.IsEOF())
	{
		if (m_cd.m_dep_id == m_delete)
		{
			m_cd.Delete();
			m_cd.Close();
			MessageBox(L"删除成功！");
			CString strsql;
			strsql.Format(L"select * from department");
			List_All(strsql);
			return;
		}
		m_cd.MoveNext();
	}
	MessageBox(L"不存在改科室，删除失败！");
}


void CDepartment::OnBnClickedButton2()
{
	// TODO: 在此添加控件通知处理程序代码
	m_delete.Empty();
	UpdateData(FALSE); //把数据更新到控件上
}
void CDepartment::List_All(CString str) {
	m_deplist.DeleteAllItems();
	//Cscore m_score;
	Department m_dep;
	try {
		if (m_dep.IsOpen())
			m_dep.Close();
		if (!m_dep.Open(CRecordset::dynaset, str)) {
			MessageBox(L"打开数据库失败", L"数据库错误", MB_OK);
			return;
		}
	}
	catch (CDBException* e) {
		e->ReportError();
	}

	int nindex = 0;
	m_dep.MoveFirst();
	while (!m_dep.IsEOF()) {
		LV_ITEM litem;
		litem.mask = LVIF_TEXT;
		litem.iItem = nindex;
		litem.iSubItem = 0;
		litem.pszText = L"";
		m_deplist.InsertItem(&litem);
		m_deplist.SetItemText(nindex, 0, m_dep.m_dep_id);
		m_deplist.SetItemText(nindex, 1, m_dep.m_dep_name);
		m_deplist.SetItemText(nindex, 2, m_dep.m_dep_address);
		m_deplist.SetItemText(nindex, 3, m_dep.m_dep_tep);
		m_deplist.SetItemText(nindex, 4, m_dep.m_dep_dir);
		m_dep.MoveNext();
		nindex++;
	}
	UpdateData(false);
	m_dep.Close();
}
